# A file to summarize information about matching when perceptions were not required to differ
## Describe the anyDA design where we do not restrict the pairs to be different in perceptions.

library(here)
library(dplyr)
library(ggplot2)

## The anyDA design:
load(here::here("Data", "wrkdatOwnMap_anyDA_new.rda"), verbose = TRUE)
load(here::here("Design", "matches_anyDA_new_nofar.rda"), verbose = TRUE)

wdat0 <- wrkdatOwnMap_new %>%
  filter(!is.na(vm_change) & !is.na(social.capital01) & !is.na(community.resp01)) %>%
  droplevels()

wdat0 <- left_join(wdat0, matches_anyDA_new_nofar)
table(is.na(wdat0$pair))
#
# FALSE  TRUE
#  3276  3110

pair_diffs_abs <- wdat0 %>%
  filter(!is.na(pair)) %>%
  group_by(pair) %>%
  summarize(
    perc_diffs = abs(diff(vm.community.norm2)),
    cohesion_diffs = abs(diff(social.capital01)),
    efficacy_diffs = abs(diff(community.resp01)),
    da_prop_vm_20pct_06 = abs(diff(da_prop_vm_20pct_06)),
    vm_change = abs(diff(vm_change)),
    vm.community.subj = abs(diff(vm.community.subj)),
    csd_pop_06 = abs(diff(csd_pop_06)),
    csd_pop_dens_06 = abs(diff(csd_pop_dens_06)),
    csd_prop_vm_20pct_06 = abs(diff(csd_prop_vm_20pct_06)),
    community.area.km = abs(diff(community_area_km))
  )

summary(pair_diffs_abs$perc_diffs)
#    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
#  0.0000  0.0700  0.1500  0.2132  0.2975  1.0000
quantile(pair_diffs_abs$perc_diffs, seq(0, 1, .1))
#   0%  10%  20%  30%  40%  50%  60%  70%  80%  90% 100%
# 0.00 0.02 0.05 0.08 0.11 0.15 0.20 0.26 0.35 0.50 1.00
mean(pair_diffs_abs$perc_diffs == 0)
# [1] 0.02747253
sum(pair_diffs_abs$perc_diffs == 0)
# [1] 45
nrow(pair_diffs_abs)
# [1] 1638

summary(pair_diffs_abs)
#       pair          perc_diffs     cohesion_diffs    efficacy_diffs  da_prop_vm_20pct_06   vm_change        vm.community.subj
#  Min.   :   1.0   Min.   :0.0000   Min.   :0.00000   Min.   :0.000   Min.   :0.000e+00   Min.   :0.000000   Min.   :0.0000
#  1st Qu.: 410.2   1st Qu.:0.0700   1st Qu.:0.08333   1st Qu.:0.125   1st Qu.:0.000e+00   1st Qu.:0.000000   1st Qu.:0.0700
#  Median : 819.5   Median :0.1500   Median :0.16667   Median :0.125   Median :0.000e+00   Median :0.004798   Median :0.1500
#  Mean   : 819.5   Mean   :0.2132   Mean   :0.16209   Mean   :0.211   Mean   :1.138e-05   Mean   :0.008586   Mean   :0.2307
#  3rd Qu.:1228.8   3rd Qu.:0.2975   3rd Qu.:0.25000   3rd Qu.:0.250   3rd Qu.:0.000e+00   3rd Qu.:0.016840   3rd Qu.:0.3000
#  Max.   :1638.0   Max.   :1.0000   Max.   :0.75000   Max.   :1.000   Max.   :1.490e-04   Max.   :0.030000   Max.   :4.1900
#    csd_pop_06      csd_pop_dens_06   csd_prop_vm_20pct_06 community.area.km
#  Min.   :      0   Min.   :   0.00   Min.   :0.00000      Min.   :    0.000
#  1st Qu.:      0   1st Qu.:   0.00   1st Qu.:0.00000      1st Qu.:    9.416
#  Median :  14328   Median :  52.77   Median :0.01165      Median :   81.999
#  Mean   : 229291   Mean   : 363.80   Mean   :0.05021      Mean   :  907.487
#  3rd Qu.: 141482   3rd Qu.: 268.09   3rd Qu.:0.05286      3rd Qu.:  452.859
#  Max.   :2499509   Max.   :4537.65   Max.   :0.54224      Max.   :36546.056
sapply(pair_diffs_abs[, -1], sd, na.rm = TRUE)
#           perc_diffs       cohesion_diffs       efficacy_diffs  da_prop_vm_20pct_06            vm_change    vm.community.subj
#         1.973898e-01         1.325089e-01         1.718370e-01         3.234516e-05         9.575996e-03         2.606530e-01
#           csd_pop_06      csd_pop_dens_06 csd_prop_vm_20pct_06    community.area.km
#         5.025997e+05         8.035788e+02         8.856363e-02         3.144180e+03

sapply(pair_diffs_abs[, -1], function(x) {
  return(quantile(x, seq(0, 1, .1), na.rm = TRUE))
})

#     perc_diffs cohesion_diffs efficacy_diffs da_prop_vm_20pct_06    vm_change vm.community.subj csd_pop_06 csd_pop_dens_06
# 0%         0.00     0.00000000          0.000        0.000000e+00 0.0000000000              0.00        0.0        0.000000
# 10%        0.02     0.00000000          0.000        0.000000e+00 0.0000000000              0.03        0.0        0.000000
# 20%        0.05     0.08333333          0.125        0.000000e+00 0.0000000000              0.05        0.0        0.000000
# 30%        0.08     0.08333333          0.125        0.000000e+00 0.0000000000              0.08     1093.3        3.311141
# 40%        0.11     0.08333333          0.125        0.000000e+00 0.0001389915              0.11     5808.2       19.008403
# 50%        0.15     0.16666667          0.125        0.000000e+00 0.0047979798              0.15    14327.5       52.774613
# 60%        0.20     0.16666667          0.250        0.000000e+00 0.0096268182              0.21    42355.4      118.619450
# 70%        0.26     0.25000000          0.250        0.000000e+00 0.0138230024              0.27    91153.5      201.215354
# 80%        0.35     0.25000000          0.375        0.000000e+00 0.0188679245              0.36   343589.8      351.068906
# 90%        0.50     0.33333333          0.500        6.067668e-05 0.0238387655              0.53   743180.0      859.810541
# 100%       1.00     0.75000000          1.000        1.489647e-04 0.0300000000              4.19  2499509.0     4537.648049
#     csd_prop_vm_20pct_06 community.area.km
# 0%           0.0000000000      1.101256e-05
# 10%          0.0000000000      1.839938e+00
# 20%          0.0000000000      5.551751e+00
# 30%          0.0005120509      1.584846e+01
# 40%          0.0059397482      3.869819e+01
# 50%          0.0116524436      8.199893e+01
# 60%          0.0209882547      1.556996e+02
# 70%          0.0382139996      2.945578e+02
# 80%          0.0710934021      7.072648e+02
# 90%          0.1715703685      1.801966e+03
# 100%         0.5422422281      3.654606e+04
#
